package com.ruoyi.project.modules.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.modules.businessUsage.domain.BusinessUsage;
import com.ruoyi.project.modules.businessUsage.mapper.BusinessUsageMapper;
import com.ruoyi.project.modules.userDataAuth.mapper.UserDataAuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.business.mapper.BusinessInfoMapper;
import com.ruoyi.project.modules.business.domain.BusinessInfo;
import com.ruoyi.project.modules.business.service.IBusinessInfoService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 业务需求信息Service业务层处理
 *
 * @author smallrain
 * @date 2021-10-28
 */
@Service
public class BusinessInfoServiceImpl implements IBusinessInfoService {
    @Autowired
    private BusinessInfoMapper businessInfoMapper;

    /**
     * 查询业务需求信息
     *
     * @param id 业务需求信息主键
     * @return 业务需求信息
     */
    @Override
    public BusinessInfo selectBusinessInfoById(Long id) {
        return businessInfoMapper.selectBusinessInfoById(id);
    }

    /**
     * 查询业务需求信息列表
     *
     * @param businessInfo 业务需求信息
     * @return 业务需求信息
     */
    @Override
    public List<BusinessInfo> selectBusinessInfoList(BusinessInfo businessInfo) {
        return businessInfoMapper.selectBusinessInfoList(businessInfo);
    }

    /**
     * 新增业务需求信息
     *
     * @param businessInfo 业务需求信息
     * @return 结果
     */
    @Override
    public int insertBusinessInfo(BusinessInfo businessInfo) {
        businessInfo.setCreateTime(DateUtils.getNowDate());
        int i = businessInfoMapper.insertBusinessInfo(businessInfo);
        insertBusinessUsage(businessInfo.getId(), businessInfo.getUsageIds());
        return i;
    }

    @Resource
    private BusinessUsageMapper businessUsageMapper;

    /**
     * 修改业务需求信息
     *
     * @param businessInfo 业务需求信息
     * @return 结果
     */
    @Override
    public int updateBusinessInfo(BusinessInfo businessInfo) {
        businessInfo.setUpdateTime(DateUtils.getNowDate());
        businessUsageMapper.removeByBusinessId(businessInfo.getId());
        insertBusinessUsage(businessInfo.getId(), businessInfo.getUsageIds());
        return businessInfoMapper.updateBusinessInfo(businessInfo);
    }

    private void insertBusinessUsage(Long id, List<Long> usageIds) {
        if (CollectionUtil.isNotEmpty(usageIds)) {
            BusinessUsage businessUsage = null;
            List<BusinessUsage> businessUsages = new ArrayList<>();
            for (Long usageId : usageIds) {
                businessUsage = new BusinessUsage();
                businessUsage.setBusinessId(id);
                businessUsage.setUsageId(usageId);
                businessUsages.add(businessUsage);
            }
            this.businessUsageMapper.saveBatch(businessUsages);
        }
    }

    @Resource
    private UserDataAuthMapper userDataAuthMapper;

    /**
     * 批量删除业务需求信息
     *
     * @param ids 需要删除的业务需求信息主键
     * @return 结果
     */
    @Override
    public int deleteBusinessInfoByIds(String ids) {
        userDataAuthMapper.removeByObjectId(Convert.toLongList(ids), "1");
        this.businessUsageMapper.remove(null, Convert.toLongList(ids));
        return businessInfoMapper.deleteBusinessInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除业务需求信息信息
     *
     * @param id 业务需求信息主键
     * @return 结果
     */
    @Override
    public int deleteBusinessInfoById(Long id) {
        return businessInfoMapper.deleteBusinessInfoById(id);
    }
}
